package br.com.controllers;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.logics.AddStudentLogic;
import br.com.logics.AddTeacherLogic;
import br.com.logics.LoginLogic;
import br.com.models.Student;
import br.com.models.Teacher;
import br.com.models.User;

@Controller
@RequestMapping("/")
public class MVCController {
			
	@RequestMapping(value={"", "login"})
	public ModelAndView toLogin(Model model) {
		
		model.addAttribute("user", new User());
		return new ModelAndView("login");
	}
	
	@RequestMapping("logout")
	public String logout(HttpSession session) {
	  
		session.invalidate();
		return "redirect:login";
	}
	
	@RequestMapping(value={"dologin"}, method=RequestMethod.POST)
	public String doLogin(@ModelAttribute("user") User user, HttpSession session) {
		
		System.out.println(user.getName());
		
		LoginLogic logic = new LoginLogic();
				
		user = logic.executeLogin(user);
		
		if(user == null) {
			
			return "redirect:login";
		} else {
			
			switch(user.getType()) {
			/* super users  */
			case 't':
				session.setAttribute("superuser", user);
				return "redirect:addteacher";
			case 's':
				session.setAttribute("superuser", user);
				return "redirect:addstudent";
			/* common users */
			case 'p':
				session.setAttribute("loguser", user);
				return "redirect:teacherhome";
			case 'e':
				session.setAttribute("loguser", user);
				return "redirect:studenthome";
			}
			/* common users  */
		}
		
		return null;
	}
	
	@RequestMapping("addteacher")
	public String addTeacher(Model model) {
		
		model.addAttribute("teacher", new Teacher());
		return "addTeacher";
	}
	
	@RequestMapping("addstudent")
	public String addStudent(Model model) {
		
		model.addAttribute("student", new Student());
		return "addStudent";
	}
	
	@RequestMapping("addteachersave")
	public String addTeacher(@ModelAttribute("teacher") Teacher teacher, 
			                       Model model, HttpSession session) {
		
		AddTeacherLogic addTeacher = new AddTeacherLogic();
		
		teacher = addTeacher.executeAddTeacher(teacher);
				
		model.addAttribute("teacher", teacher);
		
		session.removeAttribute("superuser");
		session.setAttribute("loguser", teacher);
		
		return "redirect:teacherhome";
	}
	
	@RequestMapping("addstudentsave")
	public String addStudent(@ModelAttribute("student") Student student, 
			                  Model model, HttpSession session) {
		
		AddStudentLogic addStudent = new AddStudentLogic();
		
		student = addStudent.executeAddStudent(student);
		
		model.addAttribute("student", student);
		
		session.removeAttribute("superuser");
		session.setAttribute("loguser", student);
		return "redirect:studenthome";
	}
	
	@RequestMapping("teacherhome")
	public String teacherHome() {
		
		return "teacherhome";
	}
	
	@RequestMapping("studenthome")
	public String studentHome() {
		
		return "studenthome";
	}
	
    @RequestMapping("lookingForZipcode")
    public String searchZIPCode(@ModelAttribute("teacher") Teacher teacher, Model model) {		
    	teacher.inicializeAdress(teacher.getCep());
    	
       	model.addAttribute("teacher", teacher);
		return "addTeacher";
    }
}
